package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.mapper.WorkMapper;
import com.sky.service.WorkService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkServiceImpl implements WorkService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private WorkMapper workMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    public BusinessDataVO getBusinessData() {
        //获取今日时间
        LocalDate date = LocalDate.now();
        //获取今日开始时间
        LocalDateTime begin = LocalDateTime.of(date, LocalTime.MIN);
        //获取今日结束时间
        LocalDateTime end = LocalDateTime.of(date, LocalTime.MAX);
        //创建一个map集合存储数据 复用之前写的mapper
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        //查询今日新增用户数
        Integer newUsers = userMapper.sumUserIdByMap(map);
        //今天订单完成率 1先获取总的订单数 2再获取已经完成的订单数
        Integer sum = orderMapper.sumIdByMap(map);//今日总订单数
        //如果查出为空就把订单数修改为0
        sum = sum == null ? 0 : sum;
        map.put("status", Orders.COMPLETED);//设置为完成状态
        Integer completed = orderMapper.sumIdByMap(map);//有效订单数
        completed = completed == null ? 0 : completed;
        double orderFulfillmentRate;//完成率
        //分母不能为0
        if (sum == 0) {
            orderFulfillmentRate = 0.0;
        } else {
            orderFulfillmentRate = (double) completed / sum;
        }

        //今日营业额
        Double turnover = orderMapper.sumAmountByMap(map);
        turnover = turnover == null ? 0 : turnover;
        //平均客单价 1先得到今日下单用户数 2今日营业额/今日完成下单用户
        Integer userCount = userMapper.sumUserCountByMap(map);//今日成功下单的用户数
        userCount = userCount == null ? 0 : userCount;
        double customerUnitPrice;
        if (userCount == 0) {
            customerUnitPrice = 0.0;
        } else {
            customerUnitPrice = turnover / userCount;
        }
        //有效订单数
        //创建封装对象返回结果
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .unitPrice(customerUnitPrice)
                .orderCompletionRate(orderFulfillmentRate)
                .turnover(turnover)
                .validOrderCount(completed)
                .newUsers(newUsers)
                .build();
        return businessDataVO;
    }

    /**
     * 查询区间端运营数据
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public BusinessDataVO getBusinessData2Time(LocalDateTime beginTime, LocalDateTime endTime) {
        //创建一个map集合存储数据 复用之前写的mapper
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        //查询今日新增用户数
        Integer newUsers = userMapper.sumUserIdByMap(map);
        //今天订单完成率 1先获取总的订单数 2再获取已经完成的订单数
        Integer sum = orderMapper.sumIdByMap(map);//今日总订单数
        //如果查出为空就把订单数修改为0
        sum = sum == null ? 0 : sum;
        map.put("status", Orders.COMPLETED);//设置为完成状态
        Integer completed = orderMapper.sumIdByMap(map);//有效订单数
        completed = completed == null ? 0 : completed;
        double orderFulfillmentRate;//完成率
        //分母不能为0
        if (sum == 0) {
            orderFulfillmentRate = 0.0;
        } else {
            orderFulfillmentRate = (double) completed / sum;
        }

        //今日营业额
        Double turnover = orderMapper.sumAmountByMap(map);
        turnover = turnover == null ? 0 : turnover;
        //平均客单价 1先得到今日下单用户数 2今日营业额/今日完成下单用户
        Integer userCount = userMapper.sumUserCountByMap(map);//今日成功下单的用户数
        userCount = userCount == null ? 0 : userCount;
        double customerUnitPrice;
        if (userCount == 0) {
            customerUnitPrice = 0.0;
        } else {
            customerUnitPrice = turnover / userCount;
        }
        //有效订单数
        //创建封装对象返回结果
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .unitPrice(customerUnitPrice)
                .orderCompletionRate(orderFulfillmentRate)
                .turnover(turnover)
                .validOrderCount(completed)
                .newUsers(newUsers)
                .build();
        return businessDataVO;
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Integer startingSale = workMapper.getSetmealCount(1);//起售
        Integer stoppingSale = workMapper.getSetmealCount(0);//停售
        //构建返回封装对象
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(startingSale)
                .discontinued(stoppingSale)
                .build();
        return setmealOverViewVO;
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Integer sold = workMapper.getDishCount(1);//起售
        Integer discontinued = workMapper.getDishCount(0);//停售
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return dishOverViewVO;
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        Integer cancelledOrders = workMapper.getOrderOverView(Orders.CANCELLED);//已取消
        Integer completedOrders = workMapper.getOrderOverView(Orders.COMPLETED);//已完成
        Integer deliveredOrders = workMapper.getOrderOverView(Orders.CONFIRMED);//待派送(已结接单)
        Integer waitingOrders = workMapper.getOrderOverView(Orders.TO_BE_CONFIRMED);//待接单
        Integer allOrders = workMapper.getAllOrders();//所有订单数
        //返回对象
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
        return orderOverViewVO;
    }
}
